<?
// START PREMIUM PICTURES BY BENWORLD
global $ads_tbl,$pic_tbl,$pic_tbl;
global $limit,$number_of_ads_per_page,$ads_tbl,$cat_tbl,$usr_tbl;
global $q_tbl,$offset,$set_list_img,$set_list_img_fp;
global $set_general_override,$set_fav_override,$set_bestrated_override;
global $set_latest_override,$set_frontpage_override,$set_premium_limit;
global $set_myitems_override,$set_top_override,$set_search_override,$set_mostviewed_override;
global $set_top_override,$validation,$cat_tpl,$language,$set_seperator;
include("admin/config/opt_inc.php");
$tplPremium=new TplLoad;
$tplPremium->assign("set_premium_pictureads_fp",$set_premium_fp);
$tplPremium->assign("set_premium_pictureads_cat",$set_premium_cat);
$tplPremium->assign("set_premium_picture_categories",$set_premium_picture_categories);
$tplPremium->assign("set_premium_picture_only_frontpage",$set_premium_picture_only_frontpage);

// Get important input values
$catid=round(getParam("catid","")); //Category id

$fArray = array();
$tplPremium->assign("catid",$catid);

$val_string = "";
if ($set_validation) // If ad validation is on, only show ad_is_valid=1 ads
    $val_string = " AND ad_is_validated = 1";
/*JMR: validación de avisos no PAGOS */
$val_string .= " AND ad_enabled = 1";	
// START CATFATHERID
$sql_string = "SELECT cat_id FROM $cat_tbl WHERE cat_fatherid=$catid";
$r1 = q($sql_string);
$i=0;
while ($r2 = mysql_fetch_array($r1))
{
    $cat_fatherid=$r2["cat_id"];
    $cat_fatheridTB[$i]=$cat_fatherid;
    $i++;
}

if(is_array($cat_fatheridTB))
{
    foreach ($cat_fatheridTB as $id)
    {
        //while ($id =! 0)
            $sql_string = "SELECT cat_id FROM $cat_tbl WHERE cat_fatherid=$id";
            $r3 = q($sql_string);

            while ($r4 = mysql_fetch_array($r3))
            {
                $cat_fatherid=$r4["cat_id"];
                $cat_fatheridTB[$i]=$cat_fatherid;
                $i++;
            }
        //}
    }
}
$tplPremium->assign("CATFATHERID",$cat_fatheridTB);
//END CATFATHERID

//START ADS.CATFATHER.PREMIUM
$nbid="";
$var= "$ads_tbl.ad_cat_id";
    if(is_array($cat_fatheridTB))
    {
        foreach ($cat_fatheridTB as $id)
        {
            $select_temp="$var=" . $id;
            $select = $select . " OR ". $select_temp;
        }
        $select = substr($select, 4);
        $select = "AND (".$select.")";

    }
$tplPremium->assign("SELECT",$select);
//END ADS.CATFATHER.PREMIUM

// START ALLOW AD ?
$sql = "SELECT cat_allow_ads FROM $cat_tbl WHERE cat_id=$catid";
$r = q($sql);
$r = mysql_fetch_array($r);
$allow = $r["cat_allow_ads"];
// END ALLOW AD ?

    if ($catid and $set_premium_picture_categories==1 and !is_array($cat_fatheridTB))
    	$sql_str =" AND $ads_tbl.ad_cat_id=$catid";
    elseif ($catid and $set_premium_picture_categories==1 and is_array($cat_fatheridTB))
    	$sql_str = "$select";
    else
    $sql_str = "";

if ($catid==0 AND isset($fp))
{
    // Number of Premium Ads with Picture on fp
    $number_of_ads_per_page = $set_premium_fp;
    // Template to use for this page
    $tpl = "latest_frontpage_premium";
    $query = "SELECT $ads_tbl.ad_id, $pic_tbl.id,
                     pic_ad_id, ad_title, $pic_tbl.filename
              FROM $pic_tbl,$ads_tbl
    		  WHERE $pic_tbl.pic_ad_id = $ads_tbl.ad_id
              AND main=1  AND ad_is_premium=1
              $val_string
              ORDER BY RAND() LIMIT $set_premium_fp";
}
else
{
    // Number of Premium Ads with Picture anywhere exept fp and cat allow add
    $number_of_ads_per_page = $set_premium_cat;
    // Template to use for this page
    $tpl = "latest_premium";
    $query = "SELECT $ads_tbl.ad_cat_id, $ads_tbl.ad_id,
                     $pic_tbl.id, pic_ad_id,ad_title, $pic_tbl.filename
              FROM $pic_tbl, $ads_tbl
              WHERE
              $pic_tbl.pic_ad_id=$ads_tbl.ad_id
              AND main=1 AND ad_is_premium=1
              $sql_str
              $val_string
              ORDER BY RAND() LIMIT $set_premium_cat";
}

$tplPremium->assign("QUERY",$query);
$result = q ($query);
$break_counter=0;
while ($row = mysql_fetch_array($result))
{
  	$break_counter++;
	$ad_id = $row["pic_ad_id"];
	$id = $row["pic_ad_id"];
	$sitetitle = $row["ad_title"];
	$filename = $row["filename"];
	$filename=ereg_replace("tmb2","tmb1",$filename);

	$lImageList1[$i]["filename"]=$filename;
  	$lImageList1[$i]["alt"]=htmlspecialchars($sitetitle,ENT_QUOTES);
  	$lImageList1[$i]["id"]=$id;
  	$lImageList1[$i]["link"]=linkDetail($id);
	$lImageList1[$i]["break"]=$break_counter;

    if ($break_counter==$set_premium_fp)
        $break_counter=0;
    $i++;
}
$tplPremium->assign("image_list1",$lImageList1);


$tplPremium->display($tpl . ".tpl");
// END PREMIUM PICTURES BY BENWORLD
?>